import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:Map和Set
 * User: LianBao
 * Date: 2024-04-23
 * Time: 18:47
 */
class Student {

}

public class Test {

    /**
     * TreeMap、TreeSet底层:一颗二叉搜索树(红黑树)
     * HashMap、HashSet底层:哈系桶
     */


    //map--> key-value
    public static void main1(String[] args) {
        Map<String, String> map = new TreeMap<>();
        map.put("及时雨", "宋江");
        map.put("齐天大圣", "孙悟空");
//        map.put("齐天大圣", "孙悟空2");//此时会更新value,因为key只能有一个

        //1.get(key),返回值value,如果key不存在,返回null
        String val1 = map.get("及时雨");
        String val2 = map.get("齐天大圣");
        System.out.println(map.get("金角大王"));
        System.out.println(map.getOrDefault("金角大王", "没有这个key哦~"));//如果没有这个key,可以设置默认值
        System.out.println(val1);
        System.out.println(val2);

        Set<String> set = map.keySet();//将所有的key都放入set中,key不可以重复
        System.out.println(set);

        Collection<String> set1 = map.values();//返回所有的value,
        System.out.println(set1);

        Set<Map.Entry<String, String>> set2 = map.entrySet();//返回key-value整体,返回值类型:Set<Map.Entry<K, V>>
        System.out.println(set2);


        System.out.println("=========");
        for (Map.Entry<String, String> entry : set2) {
//            System.out.print(entry + " ");
            System.out.println("key:" + entry.getKey() + "\tval:" + entry.getValue());
        }
        System.out.println(map.containsKey("1"));//containsKey:是否包含指定的key
        System.out.println(map.containsValue("1"));//containsValue:是否包含指定的value
        System.out.println("=============");

        /*Map<Student, String> m = new TreeMap<>();
        m.put(new Student(), "");*///错误,Student没有实现comparable接口
//        往TreeMap中存储元素,一定要是可以比较的,(实现comparable接口/传比较器)

    }

    /**
     * 1.Map是一个接口,不能实例化一个对象,
     * 2.Map中key是唯一的,value可以重复-->二叉搜索树
     * 3.
     */


    //set -->纯key,key不能重复
    public static void main2(String[] args) {
        Set<String> set = new TreeSet<>();
        set.add("abc");
        //对于set来说,每次add都是一个Object对象
        //set不能存储重复元素
        System.out.println(set);

        Iterator<String> it = set.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
        Set<String> set1 = new TreeSet<>();
        System.out.println("====");
        int[] a = {1, 2, 3, 4};
        Set<Integer> s = new TreeSet<>();
        for (int i = 0; i < a.length; i++) {
            s.add(a[i]);
        }
        System.out.println(s);
    }


    //OJ
    //1.
    public int singleNumber(int[] nums) {
        Set<Integer> set = new TreeSet<>();
        for (int i = 0; i < nums.length; i++) {
            if (!set.contains(nums[i])) {
                set.add(nums[i]);
            } else {
                set.remove(nums[i]);
            }
        }
        return (int) set.toArray()[0];
    }

    //2.
    public int numJewelsInStones(String jewels, String stones) {
        int count = 0;
        /*Set<Character> set1 = new TreeSet<>();
        for (int i = 0; i < jewels.length(); i++) {
            set1.add(jewels.charAt(i));
        }
        for (int i = 0; i < stones.length(); i++) {
            if (set1.contains(stones.charAt(i))) {
                count++;
            }
        }*/

        for (int i = 0; i < stones.length(); i++) {
            String ch = stones.charAt(i) + "";
            if (jewels.contains(ch)) {
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        HashBucket h = new HashBucket();
        h.put(1, 11);
        h.put(4, 44);
        h.put(14, 1414);
        h.put(24, 2424);
        h.put(3, 33);
        System.out.print(h.get(1));
    }
}
